package my.io.hadoop.controller;

import cn.dev33.satoken.util.SaResult;
import lombok.RequiredArgsConstructor;
import my.io.hadoop.jobs.JobComponent;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

/**
 * 任务控制器
 *
 * @date 2024/03/27
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/job")
public class JobController {

    private final Map<String, JobComponent> jobComponentMap;

    /**
     * 运行任务
     *
     * @param jobName 任务名称
     * @param inPath  任务输入路径
     */
    @RequestMapping("/run/{jobName}")
    public SaResult run(@PathVariable String jobName, String inPath) throws IOException, InterruptedException, ClassNotFoundException {
        JobComponent job = jobComponentMap.get(jobName);
        if (job == null) {
            return SaResult.error("无效的任务名");
        }
        job.runJob(jobName, inPath);
        return SaResult.ok();
    }
}
